package com.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.entity.ElcInfotype;
import com.ssm.entity.ElcNotice;
import com.ssm.entity.SysUser;
import com.ssm.service.ElcInfotypeService;
import com.ssm.service.ElcNoticeService;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * (ElcNotice)表控制层
 *
 * @author makejava
 * @since 2021-06-01 10:40:40
 */
@RestController
@RequestMapping("elcNotice")
public class ElcNoticeController {
    /**
     * 服务对象
     */
    @Resource
    private ElcNoticeService elcNoticeService;
    @Resource
    private ElcInfotypeService elcInfotypeService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne/{id}")
    public ElcNotice selectOne(@PathVariable Integer id) {
        return this.elcNoticeService.queryById(id);
    }

    @RequestMapping("selectAll")
    public PageInfo<ElcNotice> queryAll(ElcNotice elcNotice, Integer rows, Integer page,HttpServletRequest req){
        HttpSession session= req.getSession();
        SysUser sysUser = (SysUser) session.getAttribute("sysUser");
        elcNotice.setUserId(sysUser.getId());
        PageHelper.startPage(page,rows);
        List<ElcNotice> list = elcNoticeService.queryAllByUserId(elcNotice);
        PageInfo<ElcNotice> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    @RequestMapping("update")
    public String update(@RequestBody ElcNotice elcNotice){
        elcNoticeService.update(elcNotice);
        return "success";

    }
    @RequestMapping("noticeView")
    public ModelAndView noticeView(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/notice");
        return modelAndView;
    }
    @PostMapping("insert")
    public String insert(@Validated @RequestBody ElcNotice elcNotice, BindingResult bindingResult, Model model){
        ElcNotice elcNotice1 = new ElcNotice();
        elcNotice1.setTitle(elcNotice.getTitle());
        elcNotice1.setInfoTypeId(elcNotice.getInfoTypeId());
        List<ElcNotice> elcNotices = elcNoticeService.queryAll(elcNotice1);

        if(bindingResult.hasErrors()) {
            //数据验证没有全部通过
            List<ObjectError> list = bindingResult.getAllErrors();
            model.addAttribute("errors", list);
            return "failure";
        }
        if (elcNotices.isEmpty()){
            elcNotice.setCreateTime(new Date());
            elcNoticeService.insert(elcNotice);
            return "success";
        }
        return "repet";

    }
    @RequestMapping("detailNoticeView/{id}")
    public ModelAndView detailNoticeView(@PathVariable Integer id){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("elcnotice",elcNoticeService.queryById(id));
        modelAndView.setViewName("/notice/detail");
        return modelAndView;
    }

    @RequestMapping("editNoticeView/{id}")
    public ModelAndView editNoticeView(@PathVariable Integer id,HttpServletRequest req){
        HttpSession session = req.getSession();
        SysUser sysUser = (SysUser) session.getAttribute("sysUser");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("elcnotice",elcNoticeService.queryById(id));
        modelAndView.setViewName("/notice/editNotice");
        ElcInfotype elcInfotype = new ElcInfotype();
        elcInfotype.setUser_id(sysUser.getId());
        elcInfotype.setIsEnable(1);
        modelAndView.addObject("columnList",elcInfotypeService.queryAllByUserId(elcInfotype));
        return modelAndView;
    }

    @PutMapping("edit")
    public String edit(@Validated @RequestBody ElcNotice elcNotice,BindingResult bindingResult,Model model){
        ElcNotice elcNotice1 = new ElcNotice();
        elcNotice1.setTitle(elcNotice.getTitle());
        elcNotice1.setInfoTypeId(elcNotice.getInfoTypeId());
        List<ElcNotice> elcNotices = elcNoticeService.queryAll(elcNotice1);
        if(bindingResult.hasErrors()) {
            //数据验证没有全部通过
            List<ObjectError> list = bindingResult.getAllErrors();
            for (ObjectError objectError:list){
                System.out.println(objectError.getDefaultMessage());
            }
            model.addAttribute("errors", list);
            return "failure";
        }
        if (elcNotices.isEmpty()){
            elcNotice.setUpdateTime(new Date());
            elcNoticeService.update(elcNotice);
            return "success";
        }
        return "repet";
    }
    @DeleteMapping("deleteNotice/{id}")
    public String delete(@PathVariable Integer id){
        elcNoticeService.deleteById(id);
        return "success";
    }
    @DeleteMapping("deleteMoreNotice/{ids}")
    public String deleteIds(@PathVariable Integer[] ids){
        for (Integer id:ids){
            elcNoticeService.deleteById(id);
        }
        return "success";
    }
    @RequestMapping("exportExcel")
    public List<ElcNotice> exportExcel(ElcNotice elcNotice){
        return elcNoticeService.queryAll(elcNotice);
    }


}
